package isec.amov.tp2.automaintenance;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class AdicionarCarro extends Activity {

	private DBAdapter db;
	
	//private Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_adicionar_carro);
		
		db = new DBAdapter(this);
		db.open(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_adicionar_carro, menu);
		return true;
	}
	
	public void onProximo(View v){
		int id;
		String marca;
		String modelo;
		String matricula;
		Double quilometros;
		AlertDialog.Builder bdialog;
		
		try{
			EditText tbMarca = (EditText) findViewById(R.id.tbMarca);
			marca = tbMarca.getText().toString();
			
			EditText tbModelo = (EditText) findViewById(R.id.tbModelo);
			modelo = tbModelo.getText().toString();
			
			EditText tbMatricula = (EditText) findViewById(R.id.tbMatricula);
			matricula = tbMatricula.getText().toString();
			
			EditText tbQuilometros = (EditText) findViewById(R.id.tbQuilometros);
			quilometros = Double.parseDouble(tbQuilometros.getText().toString());
			
			bdialog = new AlertDialog.Builder(this);
			if(!db.isMatriculaRepeated(matricula)){
				id = db.getNextAvailableID();
				Carro car = new Carro(id, marca, modelo, matricula, quilometros);
				db.createCarro(car);
				db.close();
				
				/*
				bdialog.setTitle("");
				bdialog.setMessage(R.string.novo_carro_sucesso);
				bdialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
			   	});
				
				AlertDialog dialog = bdialog.create();
				dialog.show();*/
				CheckBox check = (CheckBox)findViewById(R.id.checkBoxIntroducaoAutomatica);
				Log.i("teste", "add1");
				if(check.isChecked()){
					Intent intent=new Intent(this, DetalheAutomatico.class);
					intent.putExtra("id", id);
					startActivity(intent);
				}else{
					Intent intent=new Intent(this, DetalheManual.class);
					intent.putExtra("id", id);
					intent.putExtra("quilometros", quilometros);
					Log.i("teste", "add2");
					startActivity(intent);
				}
				finish();
				
			}else{
				bdialog.setTitle("");
				bdialog.setMessage(R.string.matricula_repetida);
				bdialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {					
						public void onClick(DialogInterface dialog, int which) {}
				   	});
				AlertDialog dialog = bdialog.create();
				dialog.show();
			}
			
		} catch(Exception e){
			
		}
	}
	
	public void onBtCancelar(View v)
	{
		Toast.makeText(this, R.string.carro_cancelado, Toast.LENGTH_LONG).show();
		db.close();
		finish();
	}
	
	

}
